package com.songlanyun.modules.distributionconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.DistributionException;
import org.springframework.stereotype.Service;

import java.rmi.RemoteException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.distributionconfig.dao.DistributionConfigDao;
import com.songlanyun.modules.distributionconfig.entity.DistributionConfig;
import com.songlanyun.modules.distributionconfig.service.DistributionConfigService;
import org.springframework.transaction.annotation.Transactional;


@Service("distributionConfigService")
public class DistributionConfigServiceImpl extends ServiceImpl<DistributionConfigDao, DistributionConfig> implements DistributionConfigService {

    @Override
    public DistributionConfig create(DistributionConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public DistributionConfig modify(DistributionConfig entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DistributionConfig> page = this.page(
                new Query<DistributionConfig>().getPage(params),
                new QueryWrapper<DistributionConfig>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取平台分销设置
     */
    @Override
    public DistributionConfig getConfig() {
        return this.getOne(new LambdaQueryWrapper<DistributionConfig>().last("limit 1"));
    }

    /**
     * 保存平台分销设置
     * @param distributionConfig
     * @return
     */
    @Transactional
    @Override
    public DistributionConfig set(DistributionConfig distributionConfig) {

        if(distributionConfig.getMinExtract().compareTo(distributionConfig.getMaxExtract())>=0)
        {
            throw new RRException(DistributionException.EXTRACT_VALUE_NOT_NORM);
        }

        DistributionConfig config = this.getConfig();
        if(config==null)
        {
            this.create(distributionConfig);
            return distributionConfig;
        }
        else
        {
            config.setLevel(distributionConfig.getLevel());
            config.setOpen(distributionConfig.getOpen());
            config.setMinExtract(distributionConfig.getMinExtract());
            config.setMaxExtract(distributionConfig.getMaxExtract());
            this.modify(config);
            return config;
        }

    }
}